package com.shaozi.nc.io.mq;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.apache.log4j.Logger;
import org.fusesource.stomp.jms.StompJmsTopic;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import com.shaozi.data.mq.business.bean.MQExchangeData;
import com.shaozi.data.mq.business.bus.substance.SubstanceFactory;
import com.shaozi.data.mq.business.bus.substance.SubstanceHandler;
import com.shaozi.data.mq.protocol.ConnectionBuilder;
import com.shaozi.data.mq.protocol.ConsumerLauncher;
import com.shaozi.data.mq.protocol.stomp.StompTextConsumer;
import com.shaozi.lib.logger.DBLogger;
import com.shaozi.lib.throwable.SzException;
import com.shaozi.utils.common.BeanUtils;
import com.shaozi.utils.common.JsonUtil;
import com.shaozi.utils.common.SysUtils;

public class SyncAttendanceConsumer extends StompTextConsumer implements ConsumerLauncher {

	private final Logger logger = Logger.getLogger(SyncAttendanceConsumer.class);
	private String companyId;
	private String consumerName;
	@Autowired
	private SubstanceFactory mqSubstanceFactory;

	public SyncAttendanceConsumer(ConnectionBuilder connBuilder) {
		super(connBuilder);
	}

	@Override
	protected String getDurableSubScriberName() {
		return consumerName + ".api.attendance";
	}

	@Override
	protected String getDestName() {
		return "api." + companyId + ".attendance";
	}

	public String getCompanyId() {
		return companyId;
	}

	public void setCompanyId(String companyId) {
		this.companyId = companyId;
	}

	public String getConsumerName() {
		return consumerName;
	}

	public void setConsumerName(String consumerName) {
		this.consumerName = consumerName;
	}

	@Override
	public void launch() {
		if (consumerName == null) {
			consumerName = BeanUtils.getConfig().getPropertySourceValue("mq.consumer.name", "");
		}
		addMessageListener(new MessageListenHandler());
		logger.debug(this.getClass().getSimpleName() + ",launch");

	}

	private class MessageListenHandler implements MessageListener {

		@Override
		public void onMessage(Message message) {
			TextMessage msg = (TextMessage) message;
			try {
				Destination dest = msg.getJMSDestination();
				String workJson = msg.getText();
				MQExchangeData exchangeData = JsonUtil.toObject(workJson, MQExchangeData.class);
				if (exchangeData == null) {
					DBLogger dbLogger = (DBLogger) BeanUtils.getBean(DBLogger.class);
					dbLogger.save(null, SyncAttendanceConsumer.class.getName(), workJson, null, 4);
					return;
				}
				String uuid = SysUtils.buildUuid("");
				decomposeData(exchangeData, workJson, dest, uuid);
			} catch (JMSException e) {
				logger.error(SzException.getStackMessage(e));
			} catch (Exception e) {
				logger.error(SzException.getStackMessage(e));
			}
		}
	}

	/**
	 * 分解数据
	 * 
	 * @param data
	 * @throws SzException
	 * @throws JSONException
	 */
	@SuppressWarnings({ "rawtypes" })
	private void decomposeData(MQExchangeData data, String workJson, Destination dest, String uuid)
			throws JSONException, SzException {
		String destName = ((StompJmsTopic) dest).getTopicName();
		JSONObject jsonObj = new JSONObject(workJson);
		SubstanceHandler handler = mqSubstanceFactory.create(data, jsonObj, destName, true, uuid);
		if (handler == null) {
			return;
		}
		handler.substance(data, jsonObj, destName);
	}

}
